<?php
class Controller_Admin_Package extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'appdetail';
        parent::__construct($req, $res);

        $this->_list_where = array();
        $this->_list_pagesize = 50;
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_json ($type, $param = NULL)
    {
        $ret = array('status' => 1);

        if ($type == 'packageInfo')
        {
            $pkgname = trim($param);
            $info = API_Market::appinfo($pkgname);
            if (empty($info))
                $ret = array('status' => 1);
            else
            {
                $columns = array('name', 'packageName', 'iconUrl', 'categoryName', 'categoryId', 'versionName', 'downloadCount');
                $simpleInfo = array();
                foreach ($columns as $key)
                {
                    $simpleInfo[$key] = Arr::get($info, $key);
                }
                $ret = array('status' => 0, 'package' => $simpleInfo);
            }
        }

        echo json_encode($ret);exit;
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $id = API_Package::create($post);

            $controller = $this->request->controller(); 
            $directory  = $this->request->directory();
            $directory  = empty($directory) ? '' : '/'.$directory;
            $url = "$directory/$controller/detail/$id";

            $this->request->redirect($url);
        }
        else
        {
            parent::action_create();
        }
    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            API_Package::update($post, $nid);

            $controller = $this->request->controller(); 
            $directory  = $this->request->directory();
            $directory  = empty($directory) ? '' : '/'.$directory;
            $url = "$directory/$controller/detail/$nid";

            $this->request->redirect($url);
        }
        else
        {
            $data = API_Package::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['iconUrl'] = explode('|', $data['iconUrl']);
            parent::action_update($nid);
        }

    }

    public function action_detail ($nid)
    {
        $data = API_Package::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['iconUrl'] = explode('|', $data['iconUrl']);

        parent::action_detail($nid);
    }

    public function list_get_data ($offset, $limit)
    {
        $query = DB::select('id', 'serialize_data')
            ->from('app_detail')
            ->order_by('id', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->execute();

        $rows = array();
        foreach ($query as $rs)
        {
            $info = unserialize($rs['serialize_data']);
            $info['id'] = $rs['id'];
            $rows[] = $info;
        }

        return $rows;
    }

    public function list_filter_data ( & $rows)
    {
        foreach ($rows as & $rs)
        {
            $rs['sizeShow'] = round($rs['size'] / (1024 * 1024), 2) . 'MB';
        }
        parent::list_filter_data($rows);
    }
}
